﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Data.Common;
using Data.Enum;

namespace DataAccess.FinancialManage
{
    public class Financial : DbBase, IFinancial
    {
        public ResultInfo<int> CreateProperty(int incards, int outcards, decimal inmoney, decimal outmoney, decimal inreceipt, decimal outreceipt, decimal fixmoney, int fixcard, string describe)
        {
            ResultInfo<int> result = new ResultInfo<int>();
            try
            {
                //修正输入参数
                incards = incards < 0 ? 0 : incards;
                outcards = outcards < 0 ? 0 : outcards;
                inmoney = inmoney < 0 ? 0 : inmoney;
                outmoney = outmoney < 0 ? 0 : outmoney;
                inreceipt = inreceipt < 0 ? 0 : inreceipt;
                outreceipt = outreceipt < 0 ? 0 : outreceipt;
                if (SystemInfo.OperateAddressId > 0)
                {
                    SqlCommand command = GetProCommand();
                    command.CommandText = "CreatePropertyInfo";
                    command.Parameters.Add("@incards", SqlDbType.Int).Value = incards;
                    command.Parameters.Add("@outcards", SqlDbType.Int).Value = outcards;
                    command.Parameters.Add("@inmoney", SqlDbType.Decimal).Value = inmoney;
                    command.Parameters.Add("@outmoney", SqlDbType.Decimal).Value = outmoney;
                    command.Parameters.Add("@inreceipt", SqlDbType.Decimal).Value = inreceipt;
                    command.Parameters.Add("@outreceipt", SqlDbType.Decimal).Value = outreceipt;
                    command.Parameters.Add("@fixmoney", SqlDbType.Decimal).Value = fixmoney;
                    command.Parameters.Add("@fixcards", SqlDbType.Int).Value = fixcard;
                    command.Parameters.Add("@operateaddress", SqlDbType.VarChar, 50).Value = SystemInfo.OperateAddress;
                    command.Parameters.Add("@operateaddressid", SqlDbType.BigInt).Value = SystemInfo.OperateAddressId;
                    command.Parameters.Add("@operatepsamid", SqlDbType.VarChar, 50).Value = SystemInfo.OperatePsamId;
                    command.Parameters.Add("@operatorid", SqlDbType.BigInt).Value = SystemInfo.OperateId;
                    command.Parameters.Add("@operater", SqlDbType.VarChar, 50).Value = SystemInfo.OperateName;
                    command.Parameters.Add("@describe", SqlDbType.Text).Value = describe;
                    command.Parameters.Add("@returnflag", SqlDbType.Int).Direction = ParameterDirection.Output;
                    command.ExecuteNonQuery();
                    int returnFlag = Convert.ToInt32(command.Parameters["@returnFlag"].Value);
                    if (returnFlag > 0)
                    {
                        result.ResultNumber = ResultEnum.Success;
                        result.Obj = returnFlag;
                    }
                }
                else
                {
                    result.ResultNumber = ResultEnum.Error;
                    result.Describe = "没有获取到当前操作站点，请联系管理员！";
                }
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }
            return result;
        }

        public ResultInfo<jsh_property> GetTopProperty(long opaddressid)
        {
            var result = new ResultInfo<jsh_property> { ResultNumber = ResultEnum.Error };
            try
            {
                string sql = string.Empty;
                var r = new jsh_property();
                sql = string.Format("select top 1 * from jsh_property where operateaddressid = {0} order by operatetime desc", opaddressid);
                DataSet ds = ExcuteSqlDataSet(sql);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    result.ResultNumber = ResultEnum.Success;
                    r = new jsh_property(ds.Tables[0].Rows[0]);
                }
                else
                {
                    result.ResultNumber = ResultEnum.Success;
                }
                result.Obj = r;

            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }

            return result;
        }

        public ResultInfo<List<jsh_property>> GetProperties(long opaddressid, DateTime beginTime, DateTime endTime)
        {
            ResultInfo<List<jsh_property>> result = new ResultInfo<List<jsh_property>>() { ResultNumber = ResultEnum.Error, Obj = new List<jsh_property>() };
            try
            {
                using (SqlCommand command = GetSqlCommand())
                {
                    string sql = string.Format(
                        "select * from jsh_property where operatetime<= '{0}' and operatetime>='{1}'", endTime, beginTime);
                    if(opaddressid>0)
                    {
                        sql += string.Format(" and operateaddressid={0}", opaddressid);
                    }
                    command.CommandText = sql;

                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    da.Fill(ds);

                    if (ds != null)
                    {
                        foreach (DataRow dataRow in ds.Tables[0].Rows)
                        {
                            jsh_property view = new jsh_property(dataRow);
                            if (view.TranByConstructFlag)
                            {
                                result.Obj.Add(view);
                            }
                        }
                        result.ResultNumber = ResultEnum.Success;
                    }
                }
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }

            return result;
        }

        public ResultInfo<DataSet> GetPropertiesDs(long opaddressid, DateTime beginTime, DateTime endTime)
        {
            ResultInfo<DataSet> result = new ResultInfo<DataSet>();
            try
            {
                string sql = string.Format(
                        "select * from jsh_property where operatetime<= '{0}' and operatetime>='{1}'", endTime, beginTime);
                if (opaddressid > 0)
                {
                    sql += string.Format(" and operateaddressid={0}", opaddressid);
                }

                DataSet ds = ExcuteSqlDataSet(sql);
                result.Obj = ds;
                result.ResultNumber = ResultEnum.Success;
            }
            catch (Exception e)
            {
                result.ResultNumber = ResultEnum.Error;
                result.Describe = e.Message;
            }
            return result;
        }
    }
}
